# _*_ coding:utf-8 _*_
import json  #json处理用
from pprint import pprint
import os

test_string = '''
{
    "name": "chenziang",
    "age": 35,
    "coding skills": ["python", "lua", "sqldb", "js"],
    "ages for school": {
        "primary school": 4,
        "middle school": 10,
        "high school": 13,
        "university": 16
    },
    "hobby": ["sports", "reading"],
    "married": false
}
'''
json_info = json.loads(test_string)
pprint (json_info)#使用pprint打印出来很整齐，还有其他API目前没有尝试。
print (type(json_info))
#{}对象顺序是无序的，[]数组是有序的
#如果要控制对象的顺序用键名称来控制,对象是json.loads后的
print json_info["name"]
print json_info["coding skills"][3][1] #这样写是js的下标为[1]，通常只会用到json里面元素的下标
content = 'chendamao'
for i in json_info:
    content = content +i #或者写+=i 这种写法很消耗内存，不过可以用来测试，嵌入一些计算性能的代码来验证
pprint (content)#不会抛错，但这个chendamao会整段插进去，不同对象之间不能合并

with open("info.json", "w") as file:
    json.dump(json_info, file)#把json_info保存为json格式

f ="info.json"
with open(f,'r') as file:#然后读出来
    if (f):
        print file.read()
    else:
        print "error"

with open("info.json") as file:
    pprint(json.load(file))#读取文件并且转换

os.remove("info.json") #删除这个文件
